#!/usr/bin/perl

require "./quotamin-lib.pl";
&ReadParse();

my ($dir, $username, $lable, $sortmode, $mode, $status, $h_unit, $s_unit, $h_input_num, $s_input_num, $tmp_username);
my ($hblocks, $ublocks, $sblocks, $ufiles, $hfiles, $sfiles, @u_quot, $error_msg, @line);
my ($logminor, $logpriority, $event, $formatstr);

$username = $in{"s_username"};
$dir = $in{"fsdir"};
$lable = $in{"vlable"};
$sortmode = $in{"sortmode"};
$mode = $in{"checkedmode"};
$status = $in{"limitstatus"};
$h_unit = $in{"h_unit"};
$s_unit = $in{"s_unit"};
$h_input_num = $in{"h_input_num"};
$s_input_num = $in{"s_input_num"};
$error_msg = "";

#$username =~ s/\\/\\\\/;

&foreign_require("quota", "quota-lib.pl");
#@u_quot = ($ublock, sblock, hblock, ufiles, sfiles, hfiles)
@u_quot =  &foreign_call("quota", "user_quota", $username, $dir);

$ublocks = $u_quot[0]; $hblocks = $u_quot[1]; $sblocks = $u_quot[2];
$ufiles = $u_quot[3]; $hfiles = $u_quot[4]; $sfiles = $u_quot[5];

if($status eq "ok")
{
	$error_msg = &check_input_num($h_input_num, $h_unit);
	unless($error_msg)
	{
		$error_msg = &check_input_num($s_input_num, $s_unit);
	}
	
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'quotamin_naslog_event_saveuserquota_failed_by_num_err_format'};
		$formatstr = sprintf $text{'quotamin_naslog_saveuserquota_failed_by_num_err_format'}, $username, $lable;	
	}

	unless($error_msg)
	{
		$sblocks = &KMG_str_to_blocks($s_input_num, $s_unit);
		$hblocks = &KMG_str_to_blocks($h_input_num, $h_unit);
		$error_msg = &check_blocks_input($hblocks, $sblocks);

		if($error_msg)
		{
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'quotamin_naslog_event_saveuserquota_failed_by_sblock_bigto_hblock'};
			$formatstr = sprintf $text{'quotamin_naslog_saveuserquota_failed_by_sblock_bigto_hblock'}, $username, $lable;
		}
	}

	unless($error_msg)
	{
		$sblocks = &KMG_str_to_blocks($s_input_num, $s_unit);
		$hblocks = &KMG_str_to_blocks($h_input_num, $h_unit);

		$sblocks = sprintf("%.0f", $sblocks);
		$hblocks = sprintf("%.0f", $hblocks);
		if ($hblocks <= 4)
		{
			$hblocks = 4;
		}
		if ($sblocks <= 4)
		{
			$sblocks = 4;
		}
	}
}
else
{
	$sblocks = 0;
	$hblocks = 0;
}


if($error_msg)
{
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	
	&error_setup($text{"error_title_save_user_quota"});
	&error($error_msg);
}
else
{
	#&header($text{'index_title'}, "", undef, 0, 1);

	$tmp_username = $username;

	@line = split(/\\+/, $username);
	$username = join("\\\\", @line);
	
	#system("./bbb.pl path=$module_root_directory user=$username fs=$dir sb=$sblocks hb=$hblocks sf=$sfiles hf=$hfiles 2>/dev/null");

	my $out = &ez_system("./bbb.pl", "path=$module_root_directory", "user=$username", "fs=$dir", "sb=$sblocks", "hb=$hblocks", "sf=0" , "hf=0");

	$error_msg = &is_savequota_failed($tmp_username, $dir, $hblocks, $sblocks);
	
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_crit'};
		$event = $text{'quotamin_naslog_event_saveuserquota_failed_in_progress'};
		$formatstr = sprintf $text{'quotamin_naslog_saveuserquota_failed_in_progress'}, $username, $lable;
		&nas_syslog($logminor, $logpriority, $event, $formatstr);

		&error_setup($text{"error_title_save_user_quota"});
		&error($error_msg);
	}
	else
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_info'};
		$event = $text{'quotamin_naslog_event_saveuserquota_success'};
		if(($hblocks == 0) && ($sblocks == 0))
		{
			$formatstr = sprintf $text{'quotamin_naslog_saveuserquota_success'}, $username, $lable, "NO_LIMIT", "NO_LIMIT";
		}
		else
		{
			$formatstr = sprintf $text{'quotamin_naslog_saveuserquota_success'}, $username, $lable, $h_input_num.$h_unit, $s_input_num.$s_unit;
		}
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
		&redirect("index.cgi");
	}
	
	#&footer("/quotamin/", $text{'index'});

}

